﻿#region Using Statements
using System.Diagnostics;
using System.Windows.Forms;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
#endregion

using Microsoft.Xna.Framework.Content;

using WinFormsContentLoading;
using WinFormsGraphicsDevice;
using StandardFramework;
using PrimativeViews;
using StandardLibraryXNA;
using System.Collections.Generic;

namespace PolarExpress
{
    public class SceneControl : GraphicsDeviceControl
    {
        ContentBuilder contentBuilder;
        ContentManager contentManager;

        Texture2D textureComponent;

        SpriteFont spritefontStandard;

        SpriteBatch _spriteBatch;

        public SpriteBatch SpriteBatch
        {
            get { return _spriteBatch; }
        }

        LineView _selectionView;

        BaseComponentPool _componentPool = null;

        public BaseComponentPool ComponentPool
        {
            get { return _componentPool; }
            set { _componentPool = value; }
        }

        protected override void Initialize()
        {
            _spriteBatch = new SpriteBatch(this.GraphicsDevice);
            
            _selectionView = new LineView(this.GraphicsDevice);
            _selectionView.Visible = false;

            Application.Idle += delegate { Invalidate(); };

            contentBuilder = new ContentBuilder();

            contentManager = new ContentManager(this.Services,
                                                contentBuilder.OutputDirectory);

            //FrmMain frmMain = this.Parent.Parent.Parent.Parent.Parent.Parent.Parent as FrmMain;
            //frmMain.SelectionEvent += new FrmMain.SelectionHandler(frmMain_SelectionEvent);

            LoadResources();
        }

        //void frmMain_SelectionEvent(object sender, SelectionEventArgs args)
        //{
        //    _selectionView.CreateBox(new Vector2(args.X, args.Y), new Vector2(args.X+args.Width, args.Y+args.Height));
        //    _selectionView.Visible = args.Visible;
        //}

        void LoadResources()
        {
            Cursor = Cursors.WaitCursor;
            contentManager.Unload();
            contentBuilder.Clear();

            LoadTextures();
            LoadFonts();

            Cursor = Cursors.Arrow;
        }

        /// <summary>
        /// Loads the textures for the SceneControl.
        /// </summary>
        void LoadTextures()
        {
            // Tell the ContentBuilder what to build.   
            contentBuilder.Add("C:\\polarexpress\\PolarExpress\\Media\\White.png", "textureComponent", null, "TextureProcessor");
            
            // Build this new model data.
            string buildError = contentBuilder.Build();

            if (string.IsNullOrEmpty(buildError))
            {
                // If the build succeeded, use the ContentManager to
                // load the temporary .xnb file that we just created.
                textureComponent = contentManager.Load<Texture2D>("textureComponent");
            }
            else
            {
                // If the build failed, display an error message.
                MessageBox.Show(buildError, "Error");
            }
        }

        /// <summary>
        /// Loads the textures for the SceneControl.
        /// </summary>
        void LoadFonts()
        {
            // Tell the ContentBuilder what to build.
            contentBuilder.Clear();
            contentBuilder.Add("C:\\polarexpress\\PolarExpress\\Media\\StandardFont.spritefont", "StandardFont", null, "FontDescriptionProcessor");

            // Build this new model data.
            string buildError = contentBuilder.Build();

            if (string.IsNullOrEmpty(buildError))
            {
                // If the build succeeded, use the ContentManager to
                // load the temporary .xnb file that we just created.
                spritefontStandard = contentManager.Load<SpriteFont>("StandardFont");
            }
            else
            {
                // If the build failed, display an error message.
                MessageBox.Show(buildError, "Error");
            } 
        }

        protected override void Draw()
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            _spriteBatch.Begin();

            foreach (BaseComponent c in _componentPool)
            {
                BoundComponentXNAData bound = c.GetComponent(typeof(BoundComponentXNAData)) as BoundComponentXNAData;
                
                if(bound != null)
                    _spriteBatch.Draw(textureComponent, bound.BoundXNA, Color.White);
                
            }

            foreach (BaseComponent c in _componentPool)
            {
                //List<BaseComponent> highlights = c.GetComponents(typeof(BoundComponentXNAGraphics));
                BaseComponent highlight = c.GetComponent(typeof(BoundComponentXNAGraphics));

                if (highlight != null)
                    highlight.Update(0);

            }

            _selectionView.Draw(_spriteBatch);

            _spriteBatch.DrawString(spritefontStandard, "PolarExpress", Vector2.Zero, Color.Yellow); 

            _spriteBatch.End();
        }
    }
}
